package com.penghaisoft.microwms.product.web.controller.insidewarehouse;

import lombok.extern.slf4j.Slf4j;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.service.IWmsCommonService;
import com.penghaisoft.microwms.common.util.CommonUtils;
import com.penghaisoft.microwms.common.wms.model.ProCountingTask;
import com.penghaisoft.microwms.product.service.IProCountingTaskService;


@RestController
@RequestMapping(value = "/proCountingTask")
@Slf4j
public class ProCountingTaskController extends BaseController {
	
		@Autowired
		private IProCountingTaskService proCountingTaskService;
		
		@Autowired
		private IWmsCommonService wmsCommonService;

	  /**
	   * 新增记录
	   * @param proCountingTask
	   * @return
	   */
		@PostMapping("c")
		public TransResult create(ProCountingTask proCountingTask) {
			String countingCode = wmsCommonService.getProShortSeq5(Constant.SEQ_PREFIX_SHORT_PD, Constant.SEQ_PREFIX_SHORT_REDIS_MAT_PD, 1).get(0);
			proCountingTask.setCountingCode(countingCode);
			proCountingTask.setCreateBy(getLoginName());
			Date date = new Date();
			proCountingTask.setGmtCreate(date);
			proCountingTask.setActiveFlag("1");
			proCountingTask.setStauts("0");
			/*//表单类型为空，为合同号盘点
			if(proCountingTask.getModelType() == null||proCountingTask.getModelType().equals("")){
				proCountingTask.setCountingType("0");
			}else{
				proCountingTask.setCountingType("1");
			}*/
			if(proCountingTask.getModel()==null||proCountingTask.getModel().equals("")){
				proCountingTask.setCountingType("0");
			}else{
				proCountingTask.setCountingType("1");
			}
			Resp resp =  proCountingTaskService.create(proCountingTask);
			return fromResp(resp);
		}

	  /**
	   * 删除记录（逻辑删除active_flag=0)
	   * @param proCountingTask
	   * @return
	   */
		@PostMapping("d")
		public TransResult delete(ProCountingTask proCountingTask) {
			proCountingTask.preUpdate();
			Resp resp =  proCountingTaskService.delete(proCountingTask);
			return fromResp(resp);
		}

	  /**
	   * 查询列表
	   * @param pager
	   * @return
	   */
		@PostMapping("list")
		public TransResult list(@RequestParam(name = "page", defaultValue = "1") int page,
                            @RequestParam(name = "rows", defaultValue = "10") int rows,ProCountingTask proCountingTask) {
			Pager<ProCountingTask> result = proCountingTaskService.findListByCondition(page,rows,proCountingTask);
			return success(result);
		}

	  /**
	   * 查询单条
	   * @param id
	   * @return
	   */
		@GetMapping("r/{id}")
		public TransResult queryById(@PathVariable String id) {
			ProCountingTask  proCountingTask = proCountingTaskService.findById(id);
			return success(proCountingTask);
		}

	  /**
	   * 修改记录
	   * @param proCountingTask
	   * @return
	   */
		@PostMapping("u")
		public TransResult update(ProCountingTask proCountingTask) {
			proCountingTask.preUpdate();
			Resp resp =  proCountingTaskService.update(proCountingTask);
	    	return fromResp(resp);
		}

}
